Java 正则表达式替换小心: $ / 符号

Categories: Java; Tagged with: ; @ September 3rd, 2011 14:34

尝试使用正则表达式处理内容时, 需要小心替换字符串中是否包含:$ or /, 譬如:

Pattern pattern = Pattern.compile(“\\{C0\\}”);
Matcher matcher = pattern.matcher(“Price: [{C0}].”);
System.out.println(matcher.replaceAll(“€6.99”));
System.out.println(matcher.replaceAll(“$6.99”));
输出:
Price: [€6.99].
Exception in thread “main” java.lang.IndexOutOfBoundsException: No group 6
at java.util.regex.Matcher.group(Unknown Source)
at java.util.regex.Matcher.appendReplacement(Unknown Source)
at java.util.regex.Matcher.replaceAll(Unknown Source)
at TestExcel2Xml.main(TestExcel2Xml.java:10)

可见第一个replaceAll是正常工作的, 但第二个中的美元符号就出问题了.

Java API:
Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

可以使用Matcher.quoteReplacement(String)对替换内容进行预先处理: (API)
Returns a literal replacement String for the specified String. This method produces a String that will work use as a literal replacement s in the appendReplacement method of the Matcher class. The String produced will match the sequence of characters in s treated as a literal sequence. Slashes (‘\’) and dollar signs (‘$’) will be given no special meaning.

修改为:

Pattern pattern = Pattern.compile(“\\{C0\\}”);
Matcher matcher = pattern.matcher(“Price: [{C0}].”);
System.out.println(matcher.replaceAll(“€6.99”));
System.out.println(matcher.replaceAll(Matcher.quoteReplacement(“$6.99”)));

正确输出:

Price: [€6.99].
Price: [$6.99].

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.